package tasklogic

import (
	"common/comCopier"
	"common/dao/model"
	"context"
	"github.com/pkg/errors"
	"gorm.io/gorm"

	"task-rpc-server/internal/svc"
	"task-rpc-server/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type DetailsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewDetailsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DetailsLogic {
	return &DetailsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *DetailsLogic) Details(in *pb.ID) (*pb.TaskTableResp, error) {
	// todo: add your logic here and delete this line
	var (
		data = model.TaskTable{}
		resp = pb.TaskTableResp{}
	)
	db := l.svcCtx.DB.Table(model.TableNameTaskTable)
	err := db.Transaction(func(tx *gorm.DB) error {
		err := tx.Where("id = ?", in.Id).Where("is_del=?", 0).Find(&data).Error
		if err != nil {
			return errors.New("未查询到此分类:" + in.Id)
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	err = comCopier.CopyWithOption(&resp, data)
	if err != nil {
		return nil, err
	}
	return &resp, nil
}
